<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;
use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\web\controller\Express;
use think\Db;

/**
 * 支付控制器
 * @package app\index\controller
 */
class PayConfig extends Auth
{
    public $noNeedLogin = [];

    /**
     *获取付款列表
     * @return array
     */
    public function lists()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $data = Db::name('qt_pay_type')
                ->field('p.*,t.index_show,u.real_name')
                ->alias('t')
                ->leftJoin('qt_pay_config p','p.pay_type_id = t.id')
                ->leftJoin('qt_users u','u.id = p.user_id')
                ->where(['t.store_id'=>$userInfo['store_id']])
                ->order('p.sort asc')
                ->select();
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }




    /**
     *获取付款列表
     * @return array
     */
    public function type()
    {
        if ($this->request->isPost()) {
            $data = Db::name('qt_pay_type')
                ->field('t.id,t.name,t.index_show,t.store_id')
                ->alias('t')
                ->leftJoin('qt_pay_config p','p.pay_type_id = t.id')
                ->where(['t.store_id'=>$this->userInfo['store_id'],'t.status'=>'1','t.type'=>'2'])
                ->order('p.sort asc')
                ->select();
            $orl = Db::name('qt_pay_type')->field('id,name,index_show,store_id')->where(['store_id'=>'0','status'=>'1'])->order('id asc')->select();
            $result = array_merge($orl,$data);
            $this->result('查询成功', $result, 200);
        }
        $this->result('无效的请求', '', 204);
    }







    /**
     * 添加付款方式
     */
    public function add(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['name']) || empty($getData['type'])){
                $this->result('缺少参数', '', 204);
            }
            $getData['user_id'] = $this->userInfo['id'];
            $getData['store_id'] = $this->userInfo['store_id'];
            $getData['create_time'] = time();
            $payType = [
                'store_id'=>$this->userInfo['store_id'],
                'name'=>$getData['name'],
                'status'=>$getData['status'],
                'type'=>'2',
                'index_show'=>$getData['index_show'],
                'create_time'=>time(),
            ];
            $payTypeId = Db::name('qt_pay_type')->insertGetId($payType);
            $getData['pay_type_id'] = $payTypeId;
            $getData['create_time'] = time();
            $insert = Db::name('qt_pay_config')->insert($getData);
            if($insert){
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 编辑保存付款方式
     */
    public function save(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['name']) || empty($getData['type']) || empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $payTypeId = Db::name('qt_pay_config')->where('id',$getData['id'])->value('pay_type_id');
            $payType = [
                'store_id'=>$this->userInfo['store_id'],
                'name'=>$getData['name'],
                'status'=>$getData['status'],
                'type'=>'2',
                'index_show'=>$getData['index_show'],
            ];
            try{
                Db::startTrans();
                Db::name('qt_pay_type')->where('id',$payTypeId)->update($payType);
                Db::name('qt_pay_config')->where('id',$getData['id'])->update($getData);
                Db::commit();
            }catch (Exception $exception){
                Db::rollback();
                $this->result('删除失败','',204);
            }
            $this->result('修改成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 编辑保存付款方式
     */
    public function edit(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data= Db::name('qt_pay_config')
                ->field('c.id,c.name,c.type,c.sort,c.status,t.index_show,t.index_show')
                ->alias('c')
                ->leftJoin('qt_pay_type t','c.pay_type_id = t.id')
                ->where('c.id',$getData['id'])
                ->find();
            $this->result('获取成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 删除付款方式
     */
    public function delete(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $payTypeId = Db::name('qt_pay_config')->where('id',$getData['id'])->value('pay_type_id');
            $data = Db::name('qt_pay_config')->where('id',$getData['id'])->delete();
            Db::name('qt_pay_type')->where('id',$payTypeId)->delete();
            $this->result('删除成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }



}
